package com.retail.lottery.colalottery.aop.sign;

import com.alibaba.excel.util.StringUtils;
import com.retail.lottery.colalottery.enums.CommonEnum;
import com.retail.lottery.colalottery.enums.ErrorCodeEnum;
import com.retail.lottery.colalottery.enums.PlatformEnum;
import com.retail.lottery.colalottery.utils.DateUtil;
import com.retail.lottery.colalottery.utils.MD5Util;
import com.retail.lottery.colalottery.vo.ResultVO;
import com.retail.lottery.colalottery.vo.request.InvokeReq;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Aspect
@Component
@Order(1)
@Slf4j
public class ValidateSignInterceptor {
    private static final String timestampStr = "timestamp";
    private static final String appIdStr = "app_id";
    private static final String signStr = "sig";
    private static final String mtRequestIdStr = "mt_request_id";
    public static final String platformStr = "platform";
    private static final String separator = "|";
    private static final Long timeGap = 10 * 60 * 1000L;
    private static final List<String> methodNames = new ArrayList<String>(){{
        //add("orderDataPostBack");
    }};

    @Pointcut("execution(public * com.retail.lottery.colalottery.controller.callback.*.*(..))")
    public void Pointcut() {
    }

    @Around("Pointcut()")
    public Object around(ProceedingJoinPoint point) throws Throwable {
        //1.先校验方法名
        String methodName = point.getSignature().getName();
        if (!methodNames.contains(methodName)) {
            return point.proceed();
        }

        HttpServletRequest request = ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();
        String timestamp = request.getHeader(timestampStr);
        String sign = request.getHeader(signStr);
        String appId = request.getHeader(appIdStr);
        String mtRequestId = request.getHeader(mtRequestIdStr);
        log.info("回调header参数：timestamp: {}, appId: {}, sign: {}, mtRequestId: {}", timestamp, appId, sign, mtRequestId);
//        String platform = request.getHeader(platformStr);
//        PlatformEnum enumByName = PlatformEnum.getEnumByName(platform);
//        if (enumByName == null){
//            log.error("平台参数错误");
//            return ResultVO.error(CommonEnum.SIGN_ERROR, false);
//        }
//
//        if (StringUtils.isBlank(sign) || StringUtils.isBlank(timestamp) || StringUtils.isBlank(appId)|| StringUtils.isBlank(mtRequestId)) {
//            log.error("必传参数缺失");
//            return ResultVO.error(CommonEnum.SIGN_ERROR, false);
//        }
//
//
//        long currentTime = System.currentTimeMillis() / 1000;
//        if (!sign.equalsIgnoreCase(MD5Util.getMD5WithoutSlat(platform + separator + methodName + separator + currentTime))) {
//            LocalDateTime time = Instant.ofEpochMilli(Long.valueOf(timestamp))
//                    .atZone(ZoneId.systemDefault())
//                    .toLocalDateTime();
//            if (DateUtil.timeGap(time, LocalDateTime.now(), timeGap)
//                    || !sign.equalsIgnoreCase(MD5Util.getMD5WithoutSlat(platform + separator + methodName + separator + timestamp))) {
//                log.error("验签失败");
//                return ResultVO.error(CommonEnum.SIGN_ERROR, false);
//            }
//        }

        InvokeReq req = (InvokeReq) point.getArgs()[0];
        if(StringUtils.isNotBlank(mtRequestId)) req.setMt_request_id(mtRequestId);
        if(StringUtils.isNotBlank(appId)) req.setApp_id(appId);
        if(StringUtils.isNotBlank(sign)) req.setSig(sign);
        if(StringUtils.isNotBlank(timestamp)) req.setTimestamp(Long.valueOf(timestamp));
        return point.proceed();
    }
}
